package aclInfoCollector.view.launcher;

import gui.utils.JCoolButton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileSystemView;

import aclInfoCollector.serialisator.InfoBinder;
import aclInfoCollector.serialisator.Serialisator;

public final class DialogInfoGood extends JDialog
		implements
			ActionListener,
			PropertyChangeListener {

	class Task extends SwingWorker<Void, Void> {

		private String path;

		public Task(String s) {
			path = s;
		}

		public Void doInBackground() {

			//InfoBinder infoBinder = new InfoBinder();

//			//infoBinder.launchClassAnalysis(path);
//			//infoBinder.launchJavaAnalysis(path);
//			infoBinder.launchFractalAnalysis(path);
//			//infoBinder.launchJarAnalysis(path);
//			infoBinder.getInfos();
			
			serialisator=new Serialisator(pathIN,pathOUT);
			
			xmiPath = serialisator.getXmiPath();

			setProgress(100);
			return null;
		}

		public void done() {
			dispose();
		}
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static void main(String[] args) {
		DialogInfoGood test = new DialogInfoGood (new JFrame());
		System.out.println(test.getXmiPath());
	}
	private JProgressBar progressBar;
	private JButton launchButton;
	private JButton cancelButton;
	private JButton loadPathINButton;
	private JButton loadPathOUTButton;
	private Serialisator serialisator;
	private static JFrame frame;
	private JPanel panel;
	private JTextField textFieldIN;
	private JTextField textFieldOUT;
	private JLabel labelLAUNCH;
	private Box vb;
	private String pathIN;
	private String pathOUT;
	private String launchInfo;
		

	private String xmiPath;

	private DialogInfoGood(JFrame f) {

		super(f, "acl-InfoCollector", true);

		Dimension dimDialog = new Dimension(400, 300);
		setSize(dimDialog);
		setPreferredSize(dimDialog);
		setMaximumSize(dimDialog);
		setMinimumSize(dimDialog);
		setBackground(new Color(230,230,230));
		getContentPane().setLayout(new BorderLayout());
	
		
		
		serialisator = new Serialisator();

		panel = new JPanel();
		
		progressBar = new JProgressBar(0, 100);
		progressBar.setValue(0);
		progressBar.setStringPainted(false);

		launchButton = new JCoolButton("Launch");
		launchButton.setActionCommand("Launch");
		launchButton.addActionListener(this);
		launchButton.setEnabled(false);
		
		cancelButton = new JCoolButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(this);
		
		loadPathINButton = new JCoolButton("Browse");
		loadPathINButton.setActionCommand("loadPathIN");
		loadPathINButton.addActionListener(this);

		loadPathOUTButton = new JCoolButton("Browse");
		loadPathOUTButton.setActionCommand("loadPathOUT");
		loadPathOUTButton.addActionListener(this);
		
		pathIN="";
		pathOUT="";
		launchInfo="";
		
		textFieldIN = new JTextField(pathIN);
		textFieldOUT = new JTextField(pathOUT);
		labelLAUNCH = new JLabel(pathOUT);
		
		Dimension dimLabel = new Dimension(200,30);
		
		textFieldIN.setSize(dimLabel);
		textFieldIN.setPreferredSize(dimLabel);
		textFieldOUT.setSize(dimLabel);
		textFieldOUT.setPreferredSize(dimLabel);
		labelLAUNCH.setSize(dimLabel);
		labelLAUNCH.setPreferredSize(dimLabel);
		textFieldIN.setToolTipText(pathIN);
		textFieldOUT.setToolTipText(pathOUT);
		labelLAUNCH.setToolTipText(launchInfo);
		

		vb = Box.createVerticalBox();
		vb.add(createTitle("acl-InfoCollect"));
				
		directoryINSelectionPanel();
		directoryOUTSelectionPanel();
		vb.add(Box.createVerticalStrut(10));
		vb.add(progressBar);
		vb.add(Box.createVerticalStrut(10));
		vb.add(createBottom());
		panel.add(vb);
		getContentPane().add(panel);
		// setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
			}
		});

		pack();
		setVisible(true);

	}

	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		if (action.equals("Launch") && (!pathIN.equals(""))) {
			loadPathINButton.setEnabled(false);
			loadPathOUTButton.setEnabled(false);
			progressBar.setIndeterminate(true);
			launchButton.setEnabled(false);
			Task task = new Task(pathIN);
			task.addPropertyChangeListener(this);
			task.execute();
		} else if (action.equals("loadPathIN")) {
			try {
				pathIN = loadDirectoryToScan();
				textFieldIN.setText(pathIN);
				
				System.out.println(pathIN+"  "+pathOUT);
				
			} catch (Exception o) {
				pathIN = "";
			}
			if ( !pathIN.equals("") 
					&& !pathOUT.equals("")) {
				launchButton.setEnabled(true);
			}
			else
			{
				launchButton.setEnabled(false);
			}

		} else if (action.equals("loadPathOUT")) {
			try {
				pathOUT = selectDestination();
				textFieldOUT.setText(pathOUT);
				System.out.println(pathIN+"  "+pathOUT);
			
			} catch (Exception u) {
				pathOUT = "";
			}
			if ( !pathIN.equals("") 
					&& !pathOUT.equals("")) {
				launchButton.setEnabled(true);
		}
		else
		{
			launchButton.setEnabled(false);
		}
		}
		
		 else if (action.equals("Cancel")) {
			pathOUT = null;
			dispose();
		 }
	}

	public void directoryINSelectionPanel() {
		String s = "Path to scan";
		JPanel p  = new JPanel();
		p.setBorder(new TitledBorder(s));		
		p.add(textFieldIN);
		p.add(Box.createHorizontalStrut(30));
		p.add(loadPathINButton);
		
		vb.add(p);
	}

	public void directoryOUTSelectionPanel() {
		String s = "Path OUT";
		
		JPanel p  = new JPanel();
		p.setBorder(new TitledBorder(s));
	
		p.add(textFieldOUT);
		p.add(Box.createHorizontalStrut(30));
		p.add(loadPathOUTButton);
		vb.add(p);
	}

	public Serialisator getSerialisator() {
		return serialisator;
	}

	public String getXmiPath() {
		return xmiPath;
	}

	public void launchPanel() {
		String s = "Launch scan";
		
		JPanel p = new JPanel();
		p.setBorder(new TitledBorder(s));
		
		p.add(launchButton);
		p.add(Box.createHorizontalStrut(30));
		
		
		p.add(labelLAUNCH);
		
		
		vb.add(p);
	}
	
	
	private JComponent createBottom(){
		Box mainbox = Box.createHorizontalBox();
		mainbox.add(Box.createHorizontalGlue());
		mainbox.add(launchButton);
		mainbox.add(Box.createHorizontalStrut(20));
		mainbox.add(cancelButton);
		mainbox.add(Box.createHorizontalGlue());
		return mainbox;

	}

	public String loadDirectoryToScan() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		FileSystemView vueSytem = FileSystemView.getFileSystemView();
		File home = vueSytem.getHomeDirectory();
		chooser.setCurrentDirectory(home);
		chooser.showOpenDialog(frame);
		try {
			String result =chooser.getSelectedFile().getAbsolutePath().toString();
			pathIN=result;
			textFieldIN.setToolTipText(pathIN);
			textFieldIN.setText(result);
			return result;
		} catch (Exception e) {
			textFieldIN.setToolTipText("");
		}
		textFieldIN.setToolTipText("");
		return "";
	}

	public String selectDestination() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		FileSystemView vueSytem = FileSystemView.getFileSystemView();
		File home = vueSytem.getHomeDirectory();
		chooser.setCurrentDirectory(home);
		chooser.showOpenDialog(frame);
		try {
			String result = "";
			if (!chooser.getSelectedFile().getAbsolutePath().toString()
					.endsWith(".xmi")) {
				result = chooser.getSelectedFile().getAbsolutePath().toString()
						.concat(".xmi");
			} else {
				result = chooser.getSelectedFile().getAbsolutePath().toString();
			}
			pathOUT=result;
			textFieldOUT.setText(result);
			textFieldOUT.setToolTipText(pathOUT);
			return result;
		} catch (Exception e) {
			textFieldOUT.setToolTipText("");
		}
		textFieldOUT.setToolTipText("");
		return "";
	}

	
	public JComponent createTitle(String title){
		Box box = Box.createHorizontalBox();
		JPanel panelLabel = new JPanel(new FlowLayout());
		JLabel label = new JLabel(title);
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(250,25));
		panelLabel.setMaximumSize(new Dimension(250,25));
		box.add(panelLabel);
		return box;
	}
	
	public void setXmiPath(String xmiPath) {
		this.xmiPath = xmiPath;
	}

	/**
	 * @return the progressBar
	 */
	public JProgressBar getProgressBar() {
		return progressBar;
	}

	/**
	 * @param progressBar the progressBar to set
	 */
	public void setProgressBar(JProgressBar progressBar) {
		this.progressBar = progressBar;
	}

	/**
	 * @return the launchButton
	 */
	public JButton getLaunchButton() {
		return launchButton;
	}

	/**
	 * @param launchButton the launchButton to set
	 */
	public void setLaunchButton(JButton launchButton) {
		this.launchButton = launchButton;
	}

	/**
	 * @return the loadPathINButton
	 */
	public JButton getLoadPathINButton() {
		return loadPathINButton;
	}

	/**
	 * @param loadPathINButton the loadPathINButton to set
	 */
	public void setLoadPathINButton(JButton loadPathINButton) {
		this.loadPathINButton = loadPathINButton;
	}

	/**
	 * @return the loadPathOUTButton
	 */
	public JButton getLoadPathOUTButton() {
		return loadPathOUTButton;
	}

	/**
	 * @param loadPathOUTButton the loadPathOUTButton to set
	 */
	public void setLoadPathOUTButton(JButton loadPathOUTButton) {
		this.loadPathOUTButton = loadPathOUTButton;
	}

	/**
	 * @return the frame
	 */
	public static JFrame getFrame() {
		return frame;
	}



	/**
	 * @return the panel
	 */
	public JPanel getPanel() {
		return panel;
	}

	/**
	 * @param panel the panel to set
	 */
	public void setPanel(JPanel panel) {
		this.panel = panel;
	}


	/**
	 * @return the labelLAUNCH
	 */
	public JLabel getLabelLAUNCH() {
		return labelLAUNCH;
	}

	/**
	 * @param labelLAUNCH the labelLAUNCH to set
	 */
	public void setLabelLAUNCH(JLabel labelLAUNCH) {
		this.labelLAUNCH = labelLAUNCH;
	}

	/**
	 * @return the vb
	 */
	public Box getVb() {
		return vb;
	}

	/**
	 * @param vb the vb to set
	 */
	public void setVb(Box vb) {
		this.vb = vb;
	}

	/**
	 * @return the pathIN
	 */
	public String getPathIN() {
		return pathIN;
	}

	/**
	 * @param pathIN the pathIN to set
	 */
	public void setPathIN(String pathIN) {
		this.pathIN = pathIN;
	}

	/**
	 * @return the pathOUT
	 */
	public String getPathOUT() {
		return pathOUT;
	}

	/**
	 * @param pathOUT the pathOUT to set
	 */
	public void setPathOUT(String pathOUT) {
		this.pathOUT = pathOUT;
	}

	/**
	 * @return the launchInfo
	 */
	public String getLaunchInfo() {
		return launchInfo;
	}

	/**
	 * @param launchInfo the launchInfo to set
	 */
	public void setLaunchInfo(String launchInfo) {
		this.launchInfo = launchInfo;
	}

	/**
	 * @param serialisator the serialisator to set
	 */
	public void setSerialisator(Serialisator serialisator) {
		this.serialisator = serialisator;
	}

	public void propertyChange(PropertyChangeEvent evt) {
			
	}
	
	public static String getOutputUrl(){
	
		DialogInfoGood d = new DialogInfoGood(new JFrame());
		return d.getPathOUT();
	}

}
